<script setup lang="ts">
import { onMounted, ref, watchEffect } from 'vue'
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ACCESS_ENUM } from '@/access/accessEnum'
import Request from '@/api/axios'
import { message } from 'ant-design-vue'
import { zxcvbn } from '@zxcvbn-ts/core'
import { Dayjs } from 'dayjs'
import { HttpStatusCode } from 'axios'

interface RuleForm {
    name: string
    pwd: string
    checkpwd: string
    email: string
}

onMounted(() => {
    watchEffect(() => {
        console.log(zxcvbn(Form.pwd).score)
    })
})

const formSize = ref('default')
const ruleFormRef = ref()
const Form = reactive<RuleForm>({
    name: '',
    pwd: '',
    checkpwd: '',
    email: '',
})

const locationOptions = ['学前', '小学', '初中', '高中', '大学']

const router = useRouter()
const submitForm = async (formEl) => {
    if (!formEl) return
    try {
        const valid = await formEl.validate()
        if (valid) {
            const response = await Request.post({
                url: `/user/create`,
                data: {
                    username: Form.name,
                    password: Form.checkpwd,
                    email: Form.email + `@${select.value}.com`,
                }
            })
            if (response.code == HttpStatusCode.Ok) {
                message.success('注册成功', 2)
                router.push({ name: 'login' })
            } else {
                message.error('注册失败')
                console.log(response.msg)
            }
        } else {
            message.error('格式错误')
        }
    } catch (err) {
        message.error('注册失败')
        console.log(err)
    }
}
const select = ref('qq')

</script>

<template>
    <div class="register-container">
        <div v-for="i in 5" :key="i" :class="'layer' + i"></div>
        <div class="register-header">
            <h1>立即注册，开启精彩之旅！</h1>
            <p>加入我们，探索无限可能。</p>
        </div>
        <a-form ref="ruleFormRef" class="register-form" :model="Form" label-width="auto" :label-col="{ span: 8 }"
            :size="formSize" status-icon>
            <a-form-item label="用户名" name="name">
                <a-input v-model:value="Form.name" class="input-field" placeholder="请输入用户名" />
            </a-form-item>
            <a-form-item label="密码" name="pwd">
                <a-input v-model:value="Form.pwd" class="input-field" type="password" autocomplete="off" show-password
                    placeholder="请输入密码" />
            </a-form-item>
            <a-form-item label="确认密码" name="checkpwd">
                <a-input v-model:value="Form.checkpwd" class="input-field" type="password" autocomplete="off"
                    placeholder="请再次输入密码" />
            </a-form-item>

            <a-form-item label="邮箱">
                <a-input v-model:value="Form.email" placeholder="请输入邮箱">
                    <template #addonBefore>
                        <a-select v-model:value="select">
                            <a-select-option value="qq">QQ</a-select-option>
                            <a-select-option value="163">网易</a-select-option>
                            <a-select-option value="gmail">Google</a-select-option>
                        </a-select>
                    </template>
                    <template #addonAfter> @{{ select }}.com </template>
                </a-input>
            </a-form-item>
            <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
                <a-button type="primary" @click="submitForm(ruleFormRef)" class="register-button">注册</a-button>
            </a-form-item>
        </a-form>
    </div>
</template>

<style scoped lang="scss">
.register-container {
    background: linear-gradient(135deg, #8acee0, #70edfe);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px;
    z-index: 999;
}

.register-header {
    text-align: center;
    margin-bottom: 30px;
}

.register-header h1 {
    font-size: 36px;
    color: #1890ff;
    margin-bottom: 10px;
}

.register-header p {
    font-size: 18px;
    color: #666;
}

.register-form {
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    padding: 40px;
    width: 100%;
    max-width: 600px;
    z-index: 1000;
}

:deep(.ant-form-item-label label) {
    font-size: 16px;
    color: #333;
    font-weight: 500;
}

.input-field {
    height: 45px;
    border-radius: 8px;
    border: 1px solid #ccc;
    padding: 0 15px;
    font-size: 14px;
    transition: border-color 0.3s ease;
}

.input-field:focus {
    border-color: #1890ff;
    outline: none;
    box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

.register-button {
    height: 40px;
    border-radius: 5px;
    font-size: 16px;
    background-color: #1890ff;
    border: none;
    transition: background-color 0.3s ease;
}

.register-button:hover {
    background-color: #40a9ff;
}


@function star($n) {
  $result: '#{random(100)}vw #{random(100)}vh #fff';

  @for $i from 2 through $n {
    $result: '#{$result} , #{random(100)}vw #{random(100)}vh #fff';
  }

  @return unquote($result);
}


$n: 5;
$duration: 400s;
$count : 1000;

@for $i from 1 through $n {
  $duration: floor($duration / 2);
  $count: floor($count / 2);

  .layer#{$i} {
    position: fixed;
    width: #{$i*2}px;
    height: #{$i*2}px;
    left: 0;
    top: 0;
    border-radius: 50%;
    background: #fff;
    box-shadow: star($count);
    animation: move $duration linear infinite;
  }

  .layer#{$i}::after {
    content: '';
    position: inherit;
    width: inherit;
    height: inherit;
    border-radius: inherit;
    box-shadow: inherit;
    left: 0;
    top: 100vh;
  }

}

@keyframes move {
  to {
    transform: translateY(-100vh);
  }
}
</style>
